package temp;

import android.content.Context;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

/**
 * Created by lyy on 2016/4/27.
 * 抽象的填充类
 */
public abstract class AbsTempView extends LinearLayout     {
    private        OnTempBtClickListener mBtListener;
    private static String                TAG="zjt";
    protected int mType = ERROR;
    public static final int ISGONE   = 0;
    public static final int ERROR     = 1;
    public static final int DATA_NULL = 2;
    public static final int LOADING   = 3;

    public AbsTempView(Context context) {
        this(context, null);
    }

    public AbsTempView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    private void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(setLayoutId(), this);


        init();
    }

    protected abstract void init();

    /**
     * 如果界面有按钮，则需要对着按钮的点击事件进行监听
     *
     * @param listener
     */
    public void setBtListener(@NonNull OnTempBtClickListener listener) {
        mBtListener = listener;
    }

    protected abstract int setLayoutId();

    /**
     * 将按钮点击事件传递给TempView调用类
     *
     * @param type {@link AbsTempView}
     */
    protected void onTempBtClick(View view, int type) {
        if (mBtListener != null) {
            mBtListener.onBtTempClick(view, type);
        }
    }

    public void setType(int type) {
        mType = type;
        if (type == LOADING) {
            onLoading();
            return;
        }
        if (type == ERROR) {
            onError();
        } else if (type == DATA_NULL) {
            onNull();
        }
    }
    /**
     * 在这处理type = temp.AbsTempView#ERROR 时的逻辑
     */
    public abstract void onError();

    /**
     * 在这处理type = temp.AbsTempView#DATA_NULL 时的逻辑
     */
    public abstract void onNull();

    /**
     * 在这处理type = temp.AbsTempView#LOADING 时的逻辑
     */
    public abstract void onLoading();


    public interface OnTempBtClickListener {
        /**
         * @param type {@link AbsTempView#ERROR}, {@link AbsTempView#DATA_NULL}, {@link AbsTempView#LOADING}
         */
        public void onBtTempClick(View view, int type);
    }
}
